﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Ledict.ConditionExtract.Base;
using Ledict.ConditionExtract.BLL;
using Ledict.ConditionExtract.Common;
using Ledict.ConditionExtract.Model;
using Ledict.Utility.Common;
using Ledict.Utility.Msg;

namespace Ledict.ConditionExtract.Frm.NodeTree
{
    /// <summary>
    /// 要素顺序修改界面
    /// </summary>
    public partial class FrmZhengJuDetailConfig : BaseForm
    {
        /// <summary>
        /// 情形情节业务逻辑类
        /// </summary>
        readonly CaseCauseBLL _caseCauseBll = new CaseCauseBLL();

        SYS_CaseCauseElement_EvidRelBLL sce = new SYS_CaseCauseElement_EvidRelBLL();
        SYS_Argument_ElementRelBLL saeFocus = new SYS_Argument_ElementRelBLL();
        /// <summary>
        /// 情形情节ID
        /// </summary>
        public string CCName { get; set; }

        /// <summary>
        /// 证据名称
        /// </summary>
        public string CDName { get; set; }
        /// <summary>
        /// CdID
        /// </summary>
        public string CDID { get; set; }

        /// <summary>
        /// 节点ID
        /// </summary>
        public string ccID { get; set; }

        /// <summary>
        /// 节点名称
        /// </summary>
        public string ccName { get; set; }

        

        /// <summary>
        /// 0:证据要素 1:争议焦点
        /// </summary>
        public int TagNum { get; set; }

        public FrmZhengJuDetailConfig(Int32 tag)
        {
            InitializeComponent();
            TagNum = tag;
        }

        #region 绑定数据
        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmSuitTypeRelIndexConfig_Load(object sender, EventArgs e)
        {
            if (TagNum == 0)
            {
                dgvZhengJuRel.AutoGenerateColumns = false;
                dgvZhengJuRel.Dock = DockStyle.Fill;
                var dtRel = sce.GetSysCaseCauseElementData(CDID, CCName);
                if (dtRel != null && dtRel.Rows.Count > 0)
                {
                    dtRel.Columns.Add("CCNAME");
                    for (int i = 0; i < dtRel.Rows.Count; i++)
                    {
                        dtRel.Rows[i]["CCNAME"] = CDName;
                    }
                    dgvZhengJuRel.DataSource = PublicMethod.AddDataTableNum(dtRel);
                }
                else
                {
                    DataTable dtt = (DataTable)dgvZhengJuRel.DataSource;
                    if (dtt != null)
                    { dtt.Rows.Clear(); }
                    dgvZhengJuRel.DataSource = dtt;
                }
                dgvFocusData.Visible = false;
            }
            else if (TagNum == 1)
            {
                dgvFocusData.AutoGenerateColumns = false;
                dgvFocusData.Dock = DockStyle.Fill;
                var dtFocus = saeFocus.GetSysArgumentElementRelData(ccID);
                if (dtFocus != null && dtFocus.Rows.Count > 0)
                {
                    dtFocus.Columns.Add("ccName");
                    for (int i = 0; i < dtFocus.Rows.Count; i++)
                    {
                        dtFocus.Rows[i]["ccName"] = ccName;
                    }
                    dgvFocusData.DataSource = dtFocus;
                }
                else
                {
                    DataTable dt1 = (DataTable)dgvFocusData.DataSource;
                    if (dt1 != null)
                    {
                        dt1.Rows.Clear();
                    }
                    dgvFocusData.DataSource = dt1;
                }
                dgvZhengJuRel.Visible = false;
            }
        }
        #endregion



        /// <summary>
        /// 窗体关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmSuitTypeRelIndexConfig_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult = DialogResult.Yes;
        }

        /// <summary>
        /// 删除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDel_Click(object sender, EventArgs e)
        {
            var DeleteCER_ID = "";
            int num = 0;
            if (TagNum == 0)
            {
                for (var i = 0; i <= dgvZhengJuRel.Rows.Count - 1; i++)
                {
                    var check = (DataGridViewCheckBoxCell)dgvZhengJuRel.Rows[i].Cells["check"];
                    if (check.Value != DBNull.Value && Convert.ToBoolean(check.Value))
                    {
                        num++;
                    }
                }
            }
            else if (TagNum == 1)
            {
                for (var i = 0; i <= dgvFocusData.Rows.Count - 1; i++)
                {
                    var check = (DataGridViewCheckBoxCell)dgvFocusData.Rows[i].Cells["check1"];
                    if (check.Value != DBNull.Value && Convert.ToBoolean(check.Value))
                    {
                        num++;
                    }
                }
            }
            if (num == 0)
            {
                MsgBox.Warn("请勾选需要删除的数据后再试");
                return;
            }
            if (MessageBox.Show("删除后无法恢复，是否继续操作", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {
                    bool res = false;
                    if (TagNum == 0)
                    {
                        for (var i = 0; i <= dgvZhengJuRel.Rows.Count - 1; i++)
                        {
                            var check = (DataGridViewCheckBoxCell)dgvZhengJuRel.Rows[i].Cells["check"];
                            if (check.Value!=DBNull.Value&& Convert.ToBoolean(check.Value))
                            {
                                DeleteCER_ID = dgvZhengJuRel.Rows[i].Cells["CER_ID"].Value.ToString();
                                if (!string.IsNullOrEmpty(DeleteCER_ID))
                                {
                                    res = sce.DeleteSYSCaseCauseElementEvidRel(DeleteCER_ID);
                                }
                            }
                        }
                    }
                    else if (TagNum == 1)
                    {
                        for (var i = 0; i <= dgvFocusData.Rows.Count - 1; i++)
                        {
                            var check = (DataGridViewCheckBoxCell)dgvFocusData.Rows[i].Cells["check1"];
                            if (check.Value!=DBNull.Value&& Convert.ToBoolean(check.Value))
                            {
                                DeleteCER_ID = dgvFocusData.Rows[i].Cells["AER_ID"].Value.ToString();
                                if (!string.IsNullOrEmpty(DeleteCER_ID))
                                {
                                    res = saeFocus.DeleteSysArgumentElementRelData(DeleteCER_ID);
                                }
                            }
                        }
                    }
                        if (res)
                    {
                        MsgBox.OK("删除成功!");
                    }
                    else
                    {
                        MsgBox.Warn("删除失败!");
                    }
                }
                catch (Exception error)
                {
                    MsgBox.Warn("删除失败!" + error.Message);
                }
                FrmSuitTypeRelIndexConfig_Load(null, null);
            }

        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            int num = 0;
            if (TagNum == 0)
            {
                for (var i = 0; i <= dgvZhengJuRel.Rows.Count - 1; i++)
                {
                    var check = (DataGridViewCheckBoxCell)dgvZhengJuRel.Rows[i].Cells["check"];
                    if (check.Value != DBNull.Value && Convert.ToBoolean(check.Value))
                    {
                        num++;
                    }
                }
            }
            else if (TagNum == 1)
            {
                for (var i = 0; i <= dgvFocusData.Rows.Count - 1; i++)
                {
                    var check = (DataGridViewCheckBoxCell)dgvFocusData.Rows[i].Cells["check1"];
                    if (check.Value != DBNull.Value && Convert.ToBoolean(check.Value))
                    {
                        num++;
                    }
                }
            }
            if (num == 0)
            {
                MsgBox.Warn("请勾选需要保存的数据后再试");
                return;
            }
            if (num > 0)
            {
                try
                {
                    bool res = false;
                    if (TagNum == 0)
                    {
                        for (var i = 0; i <= dgvZhengJuRel.Rows.Count - 1; i++)
                        {
                            var check = (DataGridViewCheckBoxCell)dgvZhengJuRel.Rows[i].Cells["check"];
                            if (check.Value!=DBNull.Value&& Convert.ToBoolean(check.Value))
                            {
                                SYS_CaseCauseElement_EvidRel model = new SYS_CaseCauseElement_EvidRel();
                                model.CER_ID = dgvZhengJuRel.Rows[i].Cells["CER_ID"].Value.ToString();
                                model.CD_ID = CDID;
                                model.XmlPath = dgvZhengJuRel.Rows[i].Cells["XmlPath"].Value.ToString();
                                model.EvidStatus = dgvZhengJuRel.Rows[i].Cells["EvidStatus"].Value.ToString();
                                res = sce.UpdateSYSCaseCauseElementEvidRel(model);
                            }
                        }
                    }
                    else if (TagNum == 1)
                    {
                        for (var i = 0; i <= dgvFocusData.Rows.Count - 1; i++)
                        {
                            var check = (DataGridViewCheckBoxCell)dgvFocusData.Rows[i].Cells["check1"];
                            if (check.Value!=DBNull.Value&& Convert.ToBoolean(check.Value))
                            {
                                SYS_Argument_ElementRel model = new SYS_Argument_ElementRel();
                                model.AER_ID = dgvFocusData.Rows[i].Cells["AER_ID"].Value.ToString();
                                model.XmlPath = dgvFocusData.Rows[i].Cells["XmlPath1"].Value.ToString();
                                res = saeFocus.UpdateSysArgumentElementRelData(model);
                            }
                        }
                    }
                        if (res)
                    {
                        MsgBox.OK("保存成功!");
                    }
                    else
                    {
                        MsgBox.Warn("保存失败!");
                    }
                }
                catch (Exception error)
                {
                    MsgBox.Warn("保存失败!" + error.Message);
                }
                FrmSuitTypeRelIndexConfig_Load(null, null);
            }
        }
    }
}
